package com.tool.tcpimg;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;
import javax.swing.*;

import javafx.scene.control.ComboBox;
import org.come.Frame.ZhuFrame;
import org.come.Jpanel.FrameMessageChangeJpanel;
import org.come.control.TestMain;
import org.come.until.ScrenceUntil;

/**
 * 对话框
 *
 * @author Administrator
 *
 */
public class WlinBox extends JPanel {
    private static final long serialVersionUID = 1L;
    private List<RichLabel> labels;
    // 显示偏差
    private int deviation = 0;
    // 是否禁言 flase禁言
    private boolean Gag = true;
    // 屏幕透明度
    private float Alpha = 0.0f;
    // 判断显示位置(显示在左 true)
    private boolean display = false;
    // 聊天框的大小
    private int w = 0;
    private int h = 0;
    public static ImageIcon icon = new ImageIcon("inkImg/hongmu/2/999_png.xy2uiimg.png");
    public WlinBox() {
        labels = new ArrayList<>();
        this.setBackground(UIUtils.Color_BACK);
    }


    /** 监听 */
    public InputBean isMonitor(int x, int y) {
        int Historys = 0;
        int higs = -deviation * 22;
        for (int i = 0; i < labels.size(); i++) {
            RichLabel c = labels.get(i);
            Historys = c.getHeight();
            higs += Historys;
            if (higs >= 0 && higs >= y) {
                return c.isMonitor(x, y - (higs - Historys));
            }
        }
        return null;
    }

    @Override
    public void paint(Graphics g) {
        if (labels.size() != 0) {
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, Alpha));
            g2d.setColor(Color.BLACK);
//            icon=new ImageIcon("inkImg/hongmu/2/11_png.xy2uiimg.tips_副本.png");
//            icon=new ImageIcon("inkImg/hongmu/2/999_png.xy2uiimg.png");
//            g2d.drawImage(icon.getImage(),10, 10, 70,25, this);
            g2d.dispose();
            // 记录上一个窗体所用的高度
            int History = 0;
            int hig = -deviation * 22;
            g.translate(0, hig);
            for (int i = 0; i < labels.size(); i++) {
                Component c = labels.get(i);
                g.translate(c.getX(), History);
                History = c.getHeight();
                hig += History;
                if (hig >= 0) {
                    c.paint(g);
                }
            }
        }

    }




    public void addtext(RichLabel label, int size) {
        if (labels.size() > 100) {
            AddAndSubtract(2);
        }
        if (label != null) {
            this.labels.add(label);
        }
        Formsize(size);
        if (ScreenFull() != 0) {
            while (deviation < ScreenFull()) {
                deviation++;
            }
        }

    }

    // * //系统200 信息201 世界202 当前203 队伍204 帮派205
    // * #T 系统* #X 信息* #J 世界* #Q 当前* #D 队伍* #P 帮派
    public void addtext(String text, int size, Font font) {
        if (labels.size() > 100) {
            AddAndSubtract(2);
        }
        addText(text, size, font);
        if (ScreenFull() != 0) {
            while (deviation < ScreenFull()) {
                deviation++;
            }
        }
    }

    public void addTexts(String chatTexts, int sizes) {
        RichLabel labels = new RichLabel(chatTexts, null);
        Dimension d = labels.computeSize(sizes);
        labels.setSize(d);
        if (labels != null) {
            this.labels.add(labels);
        }
        add(labels);
        Formsize(sizes);
    }

    /**
     * 自带的字体
     *
     * @param chatText
     * @param size
     */
    public void addText(String chatText, int size) {
        RichLabel label = new RichLabel(chatText, null);
        Dimension d = label.computeSize(size);
        label.setSize(d);
        if (label != null) {
            this.labels.add(label);
        }
        add(label);
        Formsize(size);
    }
    /**
     * 自定义字体
     *
     * @param chatText
     * @param size
     */
    public RichLabel addText(String chatText, int size, Font font) {
        RichLabel label = new RichLabel(chatText, font);
        Dimension d = label.computeSize(size);
        label.setSize(d);
        if (label != null) {
            this.labels.add(label);
        }
        Formsize(size);
        return label;
    }

    /**TODO 移除式添加*/
    public RichLabel removeAddText(String chatText, int size, Font font) {
        removeAll();
        RichLabel label=labels.size()!=0?labels.get(0):null;
        labels.clear();
        if (label==null) {
            label = new RichLabel(chatText, font);
            Dimension d = label.computeSize(size);
            label.setSize(d);
        }else {
            label.setFont(font);
            label.setTextSize(chatText, size);
        }
        this.labels.add(label);
        Formsize(size);
        return label;
    }

    /**
     * 修改窗体大小
     */
    public void Formsize(int size) {
        int h = 6;
        for (int i = 0; i < labels.size(); i++) {
            h += labels.get(i).getSize().getHeight();
        }
        setSize(size, h);
    }

    public void AddAndSubtract(int type) {

        switch (type) {
            case 0:
                if (ScreenFull() != 0) {
                    if (deviation > 0) {
                        deviation--;
                    }
                }
                break;
            case 1:
                if (ScreenFull() != 0) {
                    if (deviation < ScreenFull()) {
                        deviation++;
                    }
                }
                break;
            case 2:
                removemsg();
                deviation = 0;
                Formsize(280);
                break;
            case 3:
                Gag = !Gag;
                break;
            case 4:
                RemoveForms();
                break;
            case 5:
                if (h < ScrenceUntil.Screen_y - 300) {
                    h += 20;
                }
                break;
            case 6:
                if (h > 80) {
                    h -= 20;
                }
                break;
            case 7:
                if (display) {
                    Alpha -= 0.2f;
                    if (Alpha <= 0.0f) {
                        Alpha = 0.6f;
                    }
                }

                break;
            default:
                break;
        }
    }

    /**
     * 判断是否满屏
     */
    public int ScreenFull() {
        int size = 0;
        for (int i = 0; i < labels.size(); i++) {
            size += labels.get(i).getSize().getHeight();
        }
        if (size - 22 >= h) {
            return (size - h) / 22 + 2;
        }
        return 0;
    }

    /**
     * 判断是否需要取消右窗体
     */
    public void RemoveForms() {
        if (!display) {
            Alpha = 0.6f;
            w = 300;
            h = 200;
            FrameMessageChangeJpanel.chatbox1.removemsg();
            FrameMessageChangeJpanel.chatbox2.removemsg();
            ZhuFrame.getzhuframe().getChangeJpanel().setVisible(false);// 让面板显示为不可见
            TestMain.gameJframe.setSize(ScrenceUntil.Screen_x + 6,
                    ScrenceUntil.Screen_y + 28);
        } else {
            FrameMessageChangeJpanel.chatbox.removemsg();
            Alpha = 0.0f;
            w = 300;
            h = ScrenceUntil.Screen_y / 2 - 20;
            ZhuFrame.getzhuframe().getChangeJpanel().setVisible(true);// 让面板显示为可见
            TestMain.gameJframe.setSize(ScrenceUntil.Screen_x
                    + ScrenceUntil.ChatFram_X + 6, ScrenceUntil.Screen_y + 28);
        }
        display = !display;
    }

    /**清除对话记录*/
    public void removemsg() {
        removeAll();
        labels.clear();
    }

    /**清除对话记录*/
    public void removemsgs() {
        removeAll();
        labels.clear();
    }

    public List<RichLabel> getLabels() {
        return labels;
    }

    public void setLabels(List<RichLabel> labels) {
        this.labels = labels;
    }

    public int getDeviation() {
        return deviation;
    }

    public void setDeviation(int deviation) {
        this.deviation = deviation;
    }

    public boolean isGag() {
        return Gag;
    }

    public void setGag(boolean gag) {
        Gag = gag;
    }

    public float getAlpha() {
        return Alpha;
    }

    public void setAlpha(float alpha) {
        Alpha = alpha;
    }

    public boolean isDisplay() {
        return display;
    }

    public void setDisplay(boolean display) {
        this.display = display;
    }

    public int getW() {
        return w;
    }

    public void setW(int w) {
        this.w = w;
    }

    public int getH() {
        return h;
    }

    public void setH(int h) {
        this.h = h;
    }

}
